package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.security.NoAuth;
import com.yixing.tech.common.security.UserCertificate;
import com.yixing.tech.common.service.SecurityService;
import com.yixing.tech.common.utils.DESUtil;
import com.yixing.tech.system.service.ILogService;
import com.yixing.tech.system.service.IPortalService;
import com.yixing.tech.system.service.IUserService;
import com.yixing.tech.common.vo.s3.RegisterVO;
import com.yixing.tech.common.vo.s3.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yixing tech
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/portal")
public class PortalController extends BaseControl {

    @Resource
    IPortalService portalService;
    @Resource
    IUserService userService;
    @Resource
    ILogService logService;
    @Autowired
    private SecurityService securityService;

    @NoAuth
    @PostMapping("/login")
    public Rs<Object> doLogin(@RequestBody UserVO vo) throws BusinessException {
        // 使用新方式
        String pass = DESUtil.decrypt(vo.getPassword());
        UserCertificate uc = securityService.login(vo.getLoginName(), pass);
        return Rs.ok(uc);

        // 原来的逻辑
//        vo = portalService.loginWeb(vo);
//        String token = createToken();
//        vo.setToken(token);
//        reflashApiUser(vo.clone1(), token,UserType.平台用户.name());
//        vo.setRole(null);
//        return Rs.ok(vo);
//        logLogin(vo,"web");
    }

    @PostMapping("/loginByPDA")
    public Rs<Object> doLoginByPDA(@RequestBody UserVO vo) throws BusinessException {
        // 使用新方式
        UserCertificate uc = securityService.login(vo.getUsername(), vo.getPassword());
        return Rs.ok(uc);

        // 原来的逻辑
//        vo.setPassword(DESUtil.encrypt(vo.getPassword(), DESUtil.passwordKey));
//        vo = portalService.loginWeb(vo);
//        String token = createToken();
//        vo.setToken(token);
//        reflashApiUser(vo.clone1(), token,UserType.手机用户.name());
//        vo.setRole(null);
//        return Rs.ok(vo);
//        logLogin(vo,"pda");
    }

    @GetMapping("/login/refresh")
    public Rs<Object> refresh() throws BusinessException {
        throw new BusinessException("刷新啥？");

        // 原来的逻辑
//        UserVO vo = portalService.loginRefresh(getApiUser().getId());
//        String token = createToken();
//        vo.setToken(token);
//        reflashApiUser(vo.clone1(), token,UserType.平台用户.name());
//        vo.setRole(null);
//        return Rs.ok(vo);
//        logLogin(vo,"refresh");
    }

    @PostMapping("/loginByH5")
    public Rs<Object> doLoginByH5(@RequestBody UserVO vo) throws BusinessException {
        // 使用新方式
        UserCertificate uc = securityService.login(vo.getUsername(), vo.getPassword());
        return Rs.ok(uc);

        // 原来的逻辑
//        vo = portalService.loginH5(vo);
//        String token = createToken();
//        vo.setToken(token);
//        reflashApiUser(vo.clone1(), token,UserType.手机用户.name());
//        vo.setRole(null);
//        return Rs.ok(vo);
//        logLogin(vo,"h5");
    }
    @PostMapping("/login/applet")
    public Rs<Object> doLoginApplet(@RequestBody UserVO vo) throws BusinessException {
        // 使用新方式
        UserCertificate uc = securityService.login(vo.getUsername(), vo.getPassword());
        return Rs.ok(uc);

        // 原来的逻辑
//        vo = portalService.loginApplet(vo);
//        String token = createToken();
//        vo.setToken(token);
//        reflashApiUser(vo.clone1(), token,UserType.微信用户.name());
//        vo.setRole(null);
//        return Rs.ok(vo);
//        logLogin(vo,"applet");
    }

    @GetMapping("/login/wx")
    public Rs<Object> wxLogin(@RequestParam String jsCode, @RequestParam(required = false) String tenantId, @RequestParam(required = false) String roleId) throws BusinessException {
        // 使用新方式
        throw new BusinessException("需要实现");
//

        // 原来的逻辑
//        UserVO vo = portalService.wxLogin(jsCode, tenantId, roleId);
//        String token = createToken();
//        vo.setToken(token);
//        reflashApiUser(vo.clone1(), token, UserType.微信用户.name());
//        vo.setRole(null);
//        return Rs.ok(vo);
//        logLogin(vo,"wx");
    }


    @PostMapping("/register")
    public Rs<Object> register(@RequestBody RegisterVO vo) throws BusinessException {
        userService.register(vo.getPhone(), vo.getPw(), vo.getVerificationCode());
        return Rs.ok();
    }

//    private void logLogin(UserVO vo,String type) throws BusinessException{
//        LoginLog log = new LoginLog();
//        log.setType(type);
//        log.setIp(getIp());
//        log.setTenantId(vo.getTenantId());
//        log.setIsDelete(0);
//        log.setCreateTime(new Date());
//        log.setName(vo.getName());
//        log.setAccount(vo.getLoginName());
//        logService.logLogin(log);
//    }

}
